package main

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

type LoginForm struct {
	User     string `forms:"user" binding:"required"`
	Password string `forms:"password" binding:"required,min=3,max=10"`
}

type SignUpForm struct {
	Age        uint8  `forms:"age" json:"age" binding:"gte=10,lte=130"`
	Name       string `forms:"name" json:"name" binding:"required,min=3"`
	Email      string `forms:"email" json:"email" binding:"required,email"`
	Password   string `forms:"password" json:"password" binding:"required"`
	RePassword string `forms:"repassword" json:"repassword" binding:"required,eqfield=Password"` // 跨字段验证
}

func main() {
	router := gin.Default()

	router.POST("/loginJSON", func(c *gin.Context) {
		var loginForm LoginForm
		err := c.ShouldBind(&loginForm)
		if err != nil {
			fmt.Println("err: ", err)
			c.JSON(http.StatusBadRequest, gin.H{
				"error": err.Error(),
			})
			return
		}

		c.JSON(http.StatusOK, gin.H{
			"msg": "登录成功",
		})
	})

	router.POST("/signup", func(c *gin.Context) {
		var signupForm SignUpForm
		err := c.ShouldBind(&signupForm)
		if err != nil {
			fmt.Println("err: ", err)
			c.JSON(http.StatusBadRequest, gin.H{
				"error": err.Error(),
			})
			return
		}

		c.JSON(http.StatusOK, gin.H{
			"msg": "注册成功",
		})
	})

	router.Run()
}
